package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.Personnel;
import com.community.service.PersonnelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Personnel)表控制层
 *
 * @author xn
 * @since 2021-10-05 11:16:37
 */
@Controller
@RequestMapping("personnel")
public class PersonnelController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private PersonnelService personnelService;

    @RequestMapping("personnel_list")
    public String tolist() {
        return "personnel_list";
    }


    @RequestMapping("personnel_add")
    public String toadd() {
        return "personnel_add";
    }

    /**
     * 分页查询所有数据
     *
     * @param page      分页对象
     * @param personnel 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<Personnel> page, Personnel personnel) {
        QueryWrapper<Personnel> wrapper = new QueryWrapper<>();
        wrapper.eq("1", 1);
        wrapper.like(personnel.getMemberName() != null, "member_name", personnel.getMemberName());
        wrapper.like(personnel.getPersonnelType() != null, "personnel_type", personnel.getPersonnelType());
        wrapper.between(personnel.getBeginDate() != null && personnel.getOverDate() != null,
                "personnel.create_date", personnel.getBeginDate(), personnel.getOverDate());
//        wrapper.orderByDesc("create_date");
        return success(this.personnelService.selectAll(page, wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.personnelService.getById(id));
    }

    /**
     * 返回下拉框数据
     */
    @ResponseBody
    @GetMapping("getlist")
    public R getList() {
        return success(personnelService.list());
    }

    /**
     * 新增数据
     *
     * @param personnel 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(Personnel personnel) {
        personnel.setCreateDate(new Date());
        return success(this.personnelService.save(personnel));
    }

    /**
     * 修改数据
     *
     * @param personnel 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    @ResponseBody
    public R update(Personnel personnel) {
        return success(this.personnelService.updateById(personnel));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.personnelService.removeByIds(idList));
    }

    /**
     * 三表查询人数
     *
     * @return 所有数据
     */
    @GetMapping("selectCount")
    @ResponseBody
    public R selectCount(@RequestParam("region") String region) {
        return success(this.personnelService.selectCount(region));
    }
}

